<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserNotification extends Model
{
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id', 'notification_type', 'title', 'content', 
        'is_read', 'scheduled_at', 'sent_at'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'is_read' => 'boolean',
        'scheduled_at' => 'datetime',
        'sent_at' => 'datetime',
        'created_at' => 'datetime',
    ];

    /**
     * 所属用户关系
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * 获取通知类型文本
     */
    public function getTypeTextAttribute()
    {
        $types = [
            'achievement' => '成就通知',
            'daily_challenge' => '每日挑战',
            'encouragement' => '鼓励消息',
            'comeback' => '回归提醒',
            'referral' => '引流推荐',
            'system' => '系统通知',
        ];

        return $types[$this->notification_type] ?? '未知类型';
    }

    /**
     * 获取通知图标
     */
    public function getIconAttribute()
    {
        $icons = [
            'achievement' => '🏆',
            'daily_challenge' => '🎯',
            'encouragement' => '💪',
            'comeback' => '👋',
            'referral' => '🚀',
            'system' => '📢',
        ];

        return $icons[$this->notification_type] ?? '📝';
    }

    /**
     * 检查是否已读
     */
    public function isRead()
    {
        return $this->is_read;
    }

    /**
     * 检查是否已发送
     */
    public function isSent()
    {
        return !is_null($this->sent_at);
    }

    /**
     * 检查是否需要发送
     */
    public function shouldSend()
    {
        return !$this->isSent() && 
               ($this->scheduled_at === null || $this->scheduled_at <= now());
    }

    /**
     * 标记为已读
     */
    public function markAsRead()
    {
        $this->update(['is_read' => true]);
    }

    /**
     * 标记为已发送
     */
    public function markAsSent()
    {
        $this->update(['sent_at' => now()]);
    }

    /**
     * 作用域：未读通知
     */
    public function scopeUnread($query)
    {
        return $query->where('is_read', false);
    }

    /**
     * 作用域：已发送通知
     */
    public function scopeSent($query)
    {
        return $query->whereNotNull('sent_at');
    }

    /**
     * 作用域：待发送通知
     */
    public function scopePending($query)
    {
        return $query->whereNull('sent_at')
                    ->where(function ($q) {
                        $q->whereNull('scheduled_at')
                          ->orWhere('scheduled_at', '<=', now());
                    });
    }

    /**
     * 作用域：按类型筛选
     */
    public function scopeByType($query, $type)
    {
        return $query->where('notification_type', $type);
    }
}